
class Funs {

	static isNumeric(obj) {
		return obj - parseFloat(obj) >= 0;
	}

	static isPhone(str) {
		if (!this.isNumeric(str) || str.length < 6) {
			return false;
		}
		return true;
	}

	static isEmail(str) {
		var mailReg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
		return mailReg.test(str);
	}

	static toNum(num, len = 4) {
		if (num) {
			num = Number(num);
			var m = num.toExponential().match(/\d(?:\.(\d*))?e([+-]\d+)/);
			var temp = num.toFixed(Math.max(0, (m[1] || '').length - m[2]));
			var result = temp.toString().length > len ? Math.floor(temp * Math.pow(10, len)) / Math.pow(10, len) : temp
			return result === 0 ? 0 : result
		} else {
			return 0
		}
	}

	// 向上取整
	static toNumUp(num, len = 4) {
		if (num) {
			num = Number(num);
			var m = num.toExponential().match(/\d(?:\.(\d*))?e([+-]\d+)/);
			var temp = num.toFixed(Math.max(0, (m[1] || '').length - m[2]));
			var result = temp.toString().length > len ? Math.ceil(temp * Math.pow(10, len)) / Math.pow(10, len) : temp
			return result === 0 ? 0 : parseFloat(result)
		}
	}

	// a+b
	static Add(arg1, arg2) {
		arg1.toString();
		arg2.toString();
		var arg1Arr = arg1.split("."), arg2Arr = arg2.split("."), d1 = arg1Arr.length === 2 ? arg1Arr[1] : "", d2 = arg2Arr.length === 2 ? arg2Arr[1] : "";
		var maxLen = Math.max(d1.length, d2.length);
		var m = Math.pow(10, maxLen);
		var result = Number(((arg1 * m + arg2 * m) / m).toFixed(maxLen));
		var d = arguments[2];
		return typeof d === "number" ? Number((result).toFixed(d)) : result;
	}
	// a-b
	static Sub(arg1, arg2) {
		var r1, r2, m, n;
		try {
			r1 = arg1.toString().split('.')[1].length;
		} catch (e) {
			r1 = 0;
		}
		try {
			r2 = arg2.toString().split(".")[1].length;
		} catch (e) {
			r2 = 0;
		}
		m = Math.pow(10, Math.max(r1, r2));
		n = (r1 >= r2) ? r1 : r2;
		return (Math.round(arg1 * m - arg2 * m) / m).toFixed(n);
	}
	// a/b
	static Div(arg1, arg2) {
		var r1 = arg1.toString(), r2 = arg2.toString(), m, resultVal, d = arguments[2];
		m = (r2.split(".")[1] ? r2.split(".")[1].length : 0) - (r1.split(".")[1] ? r1.split(".")[1].length : 0);
		resultVal = Number(r1.replace(".", "")) / Number(r2.replace(".", "")) * Math.pow(10, m);
		return typeof d !== "number" ? Number(resultVal) : Number(resultVal.toFixed(parseInt(d)));
	}

	// a*b
	static Mul(arg1, arg2) {
		var r1 = arg1.toString(), r2 = arg2.toString(), m, resultVal, d = arguments[2];
		m = (r1.split(".")[1] ? r1.split(".")[1].length : 0) + (r2.split(".")[1] ? r2.split(".")[1].length : 0);
		resultVal = Number(r1.replace(".", "")) * Number(r2.replace(".", "")) / Math.pow(10, m);
		return typeof d !== "number" ? Number(resultVal) : Number(resultVal.toFixed(parseInt(d)));
	}
	//获取 url 后的参数值
	static getUrl(para) {
		var paraArr = window.location.href.replace(/(.*)\?/g, '').split('&');
		for (var i = 0; i < paraArr.length; i++) {
			if (para === paraArr[i].split('=')[0]) {
				return paraArr[i].split('=')[1];
			}
		}
		return '';
	}

	// 隐藏邮箱号
	static hideEmailInfo(email) {
		if (!email) { return '' }
		if (String(email).indexOf('@') > 0) {
			let newEmail, str = email.split('@'), _s = '';

			if (str[0].length > 4) {
				_s = str[0].substr(0, 4);
				for (let i = 0; i < str[0].length - 4; i++) {
					_s += '*';
				}
			} else {
				_s = str[0].substr(0, 1);
				for (let i = 0; i < str[0].length - 1; i++) {
					_s += '*';
				}
			}
			newEmail = _s + '@' + str[1];
			return newEmail;
		} else {
			return email;
		}
	}

	// 隐藏邮箱号中间4位数
	static hideMobileInfo(mobile) {
		if (!mobile) { return '' }
		let newMobile = '';
		if (mobile.length > 7) {
			newMobile = mobile.substr(0, 3) + '****' + mobile.substr(7);
			return newMobile;
		} else {
			return mobile;
		}
	}

	// 地址缩略
	static minAddress(address) {
		if (!address) { return '' }
		let newAddress = '';
		newAddress = address.substr(0, 6) + "..." + address.substr(-6);
		return newAddress;
	}

	// 还有多少时间
	static getDateDIff(end) {
		// 判断是日期格式还是时间戳
		end = new Date(end.replace(/-/g, '/')).getTime()
		// end = new RegExp('-').test(end) ? new Date(end.replace(/-/g,'/')).getTime() : end
		let minute = 1000 * 60,
			hour = minute * 60,
			day = 24 * hour,
			now = new Date().getTime(),
			diffValue = end - now

		let dayC = diffValue / day,
			hourC = diffValue % day / hour,
			minC = diffValue % hour / minute,
			result = {}

		result.day = '' + parseInt(dayC);
		result.hour = '' + parseInt(hourC);
		result.min = '' + parseInt(minC);
		return (result.day > 0 ? result.day + '天' : '')
			+ (result.hour > 0 ? result.hour + '小时' : '')
			+ (result.min > 0 ? result.min + '分钟' : '');
	}

	// 还有多少时间
	static getDateDIffSec(end) {
		// 判断是日期格式还是时间戳
		end = new Date(end.replace(/-/g, '/')).getTime()
		let now = new Date().getTime()
		return end - now;
	}

	static getDay(end) {
		// 判断是日期格式还是时间戳
		end = new Date(end.replace(/-/g, '/')).getTime()
		// end = new RegExp('-').test(end) ? new Date(end.replace(/-/g,'/')).getTime() : end
		let minute = 1000 * 60,
			hour = minute * 60,
			day = 24 * hour,
			now = new Date().getTime(),
			diffValue = end - now

		let dayC = diffValue / day,
			hourC = diffValue % day / hour,
			minC = diffValue % hour / minute,
			result = {}

		result.day = '' + parseInt(dayC);
		result.hour = '' + parseInt(hourC);
		result.min = '' + parseInt(minC);
		return result.day > 0 ? result.day : 0
	}

	// 时间戳转日期
	static getLocalTime(nS) {
		return new Date(parseInt(nS * 1000)).toLocaleString().replace(/:\d{1,2}$/, ' ');
	}

	// 时间戳转日期 (保留秒)
	static getLocalTimeSec(data) {
		var date = new Date(Number(data));//时间戳为10位需*1000，时间戳为13位的话不需乘1000
		var Y = date.getFullYear() + '-';
		var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
		var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
		var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
		var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
		var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());

		var strDate = h + m + s;
		return strDate;//2020-07-30 01:05:54
	}

	static getDate(AddDayCount) {
		let dd = new Date();
		dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期
		let y = dd.getFullYear();
		let m =
			dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1; //获取当前月份的日期，不足10补0
		let d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate(); //获取当前几号，不足10补0
		let h = dd.getHours() < 10 ? "0" + dd.getHours() : dd.getHours(); //获取当前几号，不足10补0
		let mm = dd.getMinutes() < 10 ? "0" + dd.getMinutes() : dd.getMinutes(); //获取当前几号，不足10补0
		let s = dd.getSeconds() < 10 ? "0" + dd.getSeconds() : dd.getSeconds(); //获取当前几号，不足10补0
		return y + "." + m + "." + d;
	}
}
export default Funs;